package rtka

import (
	"dgo/plg/plgbase"
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/golog"
	"github.com/spf13/cobra"
	"gnssa/pkg/rtkacaster"
	"os"
	"os/signal"
)

func hookOSExitSignal() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	s := <-c
	golog.Infof("Got os signal:%s, will close", s)
	// 释放插件
	plgbase.DeferPlugs()
	golog.Infof("after close")
}

func init() {
	gobase.ArgFuncRegister("addRtkTaskCmd", gobase.ArgFuncOption{Catalog: "corbaRoot", Func: func(args string, tag interface{}) (val interface{}, err error) {
		var rover, base string
		var eph string = os.Getenv("rtka_eph")
		var id string
		var addRtkTask = &cobra.Command{
			Use: "rtka [execute rtk]",
			Run: func(cmd *cobra.Command, args []string) {
				if len(rover) == 0 {
					golog.Warnf("rover is empty")
					return
				}
				if len(base) == 0 {
					golog.Warnf("base is empty")
					return
				}
				if eph == "" {
					golog.Warnf("eph is empty")
					return
				}

				golog.Infof("rover:%s", rover)
				golog.Infof("base:%s", base)
				golog.Infof("eph:%s", eph)

				rtkacaster.CheckEphStart(eph)
				rtka := NewRtkAChannel(id, rover, base)
				rtka.Open()

				hookOSExitSignal()
			},
		}
		addRtkTask.Flags().StringVarP(&id, "id", "i", "R01", "")
		addRtkTask.Flags().StringVarP(&rover, "rover", "r", "", "conntype=ntripclient&connstr=")
		addRtkTask.Flags().StringVarP(&base, "base", "b", "", "conntype=ntripclient&connstr=")
		addRtkTask.Flags().StringVarP(&eph, "eph", "e", "", "conntype=ntripclient&connstr=")
		// golog.Infof("rtka load")
		return addRtkTask, nil
	}})

}
